package com.heima.article.listen;

import com.heima.common.constants.article.HotArticleConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author by jojo
 * @Date 2022/3/13
 * @Description
 */
@Component
@Slf4j
public class HotArticleScoreListener {
    @Autowired
    private StringRedisTemplate redisTemplate;


    @RabbitListener(queuesToDeclare = @Queue(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE))
    public void hotArticleScoreListener(String behaviorMess){
        log.info("文章实时更新，接受到了文章行为变化，内容为：{}",behaviorMess);
        try {
            ListOperations<String, String> listOperations = redisTemplate.opsForList();
            //将文章实时更新队列从右往左存入Redis
            listOperations.rightPush(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_LIST,behaviorMess);
            log.info("文章实时更新队列存入到Redis成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文章实时更新队列失败！！！原因是==》{}",e.getMessage());
        }
    }
}
